package org.elanfox.intellicommunity.msg.server.service.impl;

import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.MethodNotSupportedException;
import org.elanfox.intellicommunity.common.constant.RedisConstant;
import org.elanfox.intellicommunity.msg.dto.CmdWriteParam;
import org.elanfox.intellicommunity.msg.dto.RawHeartbeatDTO;
import org.elanfox.intellicommunity.msg.dto.RawTagDTO;
import org.elanfox.intellicommunity.msg.dto.RawTimeVerifyDTO;
import org.elanfox.intellicommunity.msg.ienum.CmdEnums;
import org.elanfox.intellicommunity.msg.server.mq.MsgProvider;
import org.elanfox.intellicommunity.msg.server.service.CmdNormalReader;
import org.elanfox.intellicommunity.msg.server.service.CmdNormalWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @author allan
 * @version 1.0.0
 * @date 2020/3/17
 */
@Service
@Slf4j
public class CmdNormalReaderImpl implements CmdNormalReader {
    @Autowired
    private MsgProvider msgProvider;
    @Autowired
    private CmdNormalWriter cmdNormalWriter;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public void tag(String hexStr) {
//        log.info("tag read====>" + hexStr);
        RawTagDTO rawTagDTO = new RawTagDTO(CmdEnums.Code.UPLOAD_TAG, hexStr);
//        log.info("to String ==>" + rawTagDTO.toString());
        msgProvider.sendLocationMsg(rawTagDTO);
        if (rawTagDTO.isAlarmedButton() || rawTagDTO.isBatteryNotEnough()) {
            msgProvider.sendAlarmMsg(rawTagDTO);
        }
        long triggerId = rawTagDTO.getNewTriggerId();
        if (triggerId == 0) {
            triggerId = rawTagDTO.getOldTriggerId();
        }
        //收到的tag也判定为心跳消息
        redisTemplate.opsForHash().put(RedisConstant.KEY_HEARTBEAT, String.valueOf(rawTagDTO.getDeviceAddr()), System.currentTimeMillis());
        //作为定位器的心跳监测
        redisTemplate.opsForHash().put(RedisConstant.KEY_LOCATOR_HEARTBEAT, String.valueOf(triggerId), System.currentTimeMillis());
    }

    @Override
    public void timeVerify(String hexStr, Channel channel) {
        RawTimeVerifyDTO timeVerifyDTO = new RawTimeVerifyDTO(CmdEnums.Code.TIME_VERIFY, hexStr);

        //响应
        CmdWriteParam writeParam = new CmdWriteParam();
        writeParam.setHexAddr(timeVerifyDTO.getRawHexAddr());
        cmdNormalWriter.timeVerify(writeParam, channel);

        //收到的tag也判定为心跳消息
        redisTemplate.opsForHash().put(RedisConstant.KEY_HEARTBEAT, String.valueOf(timeVerifyDTO.getDeviceAddr()), System.currentTimeMillis());
    }

    @Override
    public void heartbeat(String hexStr) {
        log.info("接收到心跳消息：" + hexStr);
        RawHeartbeatDTO heartbeatDTO = new RawHeartbeatDTO(CmdEnums.Code.HEART_BEAT, hexStr);

        // TODO: 12/29/20 20s未收到心跳包了，则判定离线
        redisTemplate.opsForHash().put(RedisConstant.KEY_HEARTBEAT, String.valueOf(heartbeatDTO.getDeviceAddr()), System.currentTimeMillis());
    }

    @Override
    public void read(CmdEnums.Code cmdCode, String hexStr) throws MethodNotSupportedException {
        switch (cmdCode) {
            case UPLOAD_TAG:
                tag(hexStr);
                break;
            case HEART_BEAT:
                heartbeat(hexStr);
                break;
            default:
                log.error("method not supported for cmd code " + cmdCode.getDesc());
                break;
        }
    }

    public static void main(String[] args) {
        String hexStr = "ffffffff68ecc102001dc09dc00824642200010905f5e0ff06000005260000000050025f6428ee";

        RawTagDTO rawTagDTO = new RawTagDTO(CmdEnums.Code.UPLOAD_TAG, hexStr);

        System.out.println(111);
    }
}
